package org.ne.springboot.utils.PaperHandleUtils.PaperContentCheckUtils;


import jakarta.servlet.http.HttpServletResponse;
import org.ne.springboot.utils.PaperHandleUtils.FontCheckUtils.FormatReportServlet;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.ne.springboot.utils.PaperHandleUtils.CommonUtils.WordUtils.extractTextFromDocx;
import static org.ne.springboot.utils.PaperHandleUtils.FontCheckUtils.FontCheckUtils.fontCheck;


/**
 * @author wey
 * @date 2025/8/25 下午9:33 08
 **/
public class PaperHandleUtils {
    private static final SparkClient SPARK_CLIENT = new SparkClient();
    private static final FormatReportServlet FRS = new FormatReportServlet();

    //spark
    public static List<Map<String, Object>> contentAnalyze(String question, MultipartFile file, String model) throws IOException {
        SPARK_CLIENT.setModel(model);
        List<String> lines = extractTextFromDocx(file);
        List<Map<String, Object>> list = new ArrayList<>();
        for (String line : lines) {
            if (line.trim().length() > 50) {

                Map<String, Object> res =  SPARK_CLIENT.processRequest(question, line);
                Map<String, Object> resMap = new HashMap<>();

                resMap.put("original", line);
                resMap.put("result", res);

                list.add(resMap);
            }
        }
        return list;
    }

    public static void fontCheckReport(MultipartFile file, HttpServletResponse response) throws Exception {
        // 设置响应头，告诉浏览器返回的是ZIP文件

        response.setContentType("application/zip");
        String encodedZipName = URLEncoder.encode("格式检查结果.zip", StandardCharsets.UTF_8);

        response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedZipName + "\"");
        // 创建一个字节数组输出流，用于暂存标红文档
        ByteArrayOutputStream markedDocStream = new ByteArrayOutputStream();
        // 创建一个字节数组输出流，用于暂存报告文档
        ByteArrayOutputStream reportStream = new ByteArrayOutputStream();

        // 用于保存格式错误信息
        Map<String, List<String>> formatErrors = null;

        try {
            // 1. 执行格式检查并生成标红文档
            // 这里需要重构你的fontCheck方法，使其能够将结果写入提供的输出流
            formatErrors = fontCheck(markedDocStream, file);

            // 2. 生成报告文档
            // 这里需要重构你的FormatReportServlet，使其能够将报告写入提供的输出流
            FRS.doGet (formatErrors, reportStream);

            // 3. 创建ZIP输出流，直接将ZIP内容写入response的输出流
            try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
                // 添加标红文档到ZIP
                ZipEntry markedDocEntry = new ZipEntry("格式检查标红文档.docx");
                zos.putNextEntry(markedDocEntry);
                zos.write(markedDocStream.toByteArray());
                zos.closeEntry();

                // 添加报告文档到ZIP
                ZipEntry reportEntry = new ZipEntry("格式错误检查报告.docx");
                zos.putNextEntry(reportEntry);
                zos.write(reportStream.toByteArray());
                zos.closeEntry();
            }

        } catch (Exception e) {
            // 异常处理
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.setContentType("text/plain;charset=UTF-8");
            response.getWriter().write("处理失败: " + e.getMessage());
            throw e;
        }
    }


}



